package com.fu.shop.service.Impl;

import com.fu.core.common.BusinessException;
import com.fu.core.common.ExceptionCode;
import com.fu.core.pojo.Admin;
import com.fu.core.service.impl.BaseServiceImpl;
import com.fu.core.util.SMSSend;
import com.fu.shop.mapper.AdminMapper;
import com.fu.shop.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;


/**
 * @author 付龙江
 * @date 2022/2/1 15:52
 */
@Service("adminService")
@Transactional(rollbackFor = Throwable.class)
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {
    /**
     * 短信验证码
     */
    private static final String LOGIN_SMS="login_sms:";

    AdminMapper adminMapper;
    RedisTemplate<String,Object> redisTemplate;
    ValueOperations<String,Object> stringOps;
    @Autowired(required = false)
    public AdminServiceImpl(RedisTemplate<String, Object> redisTemplate, AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
        this.redisTemplate = redisTemplate;
        stringOps = redisTemplate.opsForValue();
    }

    @Autowired(required = false)


    /**
     * 管理员登录
     * @param phone 管理员电话
     * @param pwd 密码
     * @return 返回管理员对象
     */
    @Override
    public Admin login(String phone, String pwd) throws Exception{
        //新建条件
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("adminPhone",phone).andEqualTo("adminPwd",pwd);
        //登录获取对象
        Admin admin = adminMapper.selectOneByExample(example);
        if (admin == null){
            throw new BusinessException(ExceptionCode.WRONG_PHONE_OR_PWD);
        }
        //登录成功  更新登陆时间
        admin.setAdminLoginTime(new Timestamp(System.currentTimeMillis()));
        adminMapper.updateByPrimaryKeySelective(admin);
        return admin;
    }

    /**
     * 忘记密码
     *
     * @param phone 电话
     * @param code  短信验证码
     * @param pwd   密码
     * @return 返回修改成功的行数
     * @throws Exception 异常处理
     */
    @Override
    public Integer forgetPwd(String phone, String code, String pwd) throws Exception {
        //验证token是否正确
        Object o = stringOps.get(LOGIN_SMS + phone);
        System.out.println(o);
        System.out.println(code);
        if (StringUtils.isEmpty(o) || !o.equals(code)){
            throw new BusinessException(ExceptionCode.WRONG_CODE);
        }
        //验证码正确后 通过手机判断用户是否存在
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("adminPhone",phone);
        Admin admin = adminMapper.selectOneByExample(example);
        if (admin == null){
            throw new BusinessException(ExceptionCode.NO_ADMIN);
        }
        admin.setAdminPwd(pwd);
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    /**
     * 短信验证码
     *
     * @param phone 电话
     * @return 返回验证字符串
     */
    @Override
    public String sendLoginSms(String phone){
        String msgCode = SMSSend.getMsgCode();
        System.out.println("++++++++验证码:"+msgCode+"+++++++++");
        //服务器这是失效时间为5分钟
        stringOps.set(LOGIN_SMS+phone,msgCode,5, TimeUnit.MINUTES);
        return msgCode;
    }
}
